package org.trackcore.handler;

import org.springframework.stereotype.Component;
import org.trackcore.model.entity.PerformancePaint;
import org.trackcore.service.PerformancePaintService;
import org.trackcore.utils.DataUtils;

import java.util.Map;

/**
* 处理绘制性能数据(first-paint/first-contentful-paint等)
* 处理器Key: "performance_paint"
*/
@Component
public class PerformancePaintHandler implements DataHandler {

    private final PerformancePaintService paintService;

    public PerformancePaintHandler(PerformancePaintService paintService) {
        this.paintService = paintService;
    }

    @Override
    public String getHandlerKey() {
        return "performance_first-paint"; // 对应type=performance, subType=paint
    }
    @Override
    public void handle(Map<String, Object> data, String logId) {
        PerformancePaint paint = PerformancePaint.builder()
                .logId(logId)
                .name(DataUtils.getString(data, "name")) // "first-paint"或"first-contentful-paint"
                .entryType(DataUtils.getString(data, "entryType"))
                .startTime(DataUtils.getDouble(data, "startTime"))
                .duration(DataUtils.getDouble(data, "duration"))
                .pageUrl(DataUtils.getString(data, "pageUrl"))
                .build();

        paintService.save(paint);
    }
}
